//
//  ImageExtension.swift
//  WangPu
//
//  Created by xiaozhi on 15/10/27.
//  Copyright © 2015年 zhixiao. All rights reserved.
//

import UIKit
extension UIImage{
    func scaleTosize(asize:CGSize)->UIImage{
        let oldSize = self.size
        let newImage:UIImage!
        var rect:CGRect = CGRectZero
        if asize.width/asize.height > oldSize.width/oldSize.height{
            rect.size.width = asize.height*(oldSize.width/oldSize.height)
            rect.size.height = asize.height;
            rect.origin.x = (asize.width - rect.size.width)/2;
            rect.origin.y = 0;
        }
        else{
            rect.size.width = asize.width;
            rect.size.height = asize.width*oldSize.height/oldSize.width;
            rect.origin.x = 0;
            rect.origin.y = (asize.height - rect.size.height)/2;
        }
        UIGraphicsBeginImageContext(asize)
        let ctx = UIGraphicsGetCurrentContext()
        CGContextSetFillColorWithColor(ctx, UIColor.clearColor().CGColor)
        UIRectFill(rect)
        self.drawInRect(rect)
        newImage =  UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}